﻿
using System;

namespace USClientKernel.MVC
{
    public abstract class ModelEvent<T> : BaseCast<ModelEvent<T>> 
        where T : BaseData<T>
    {
        private T m_data;
        public T Data { get => m_data; set => m_data = value; }
    }


    public delegate void OnModelEventHandler<T, E>(E me) 
        where T : BaseData<T>
        where E : ModelEvent<T>;


    public abstract class BaseModel<T, E> : BaseCast<BaseModel<T, E>> 
        where T : BaseData<T>
        where E : ModelEvent<T>
    {
        protected event OnModelEventHandler<T, E> EventHandler;
        public virtual void DispatchModelEvent(E me)
        {
            EventHandler?.Invoke(me);
        }

        public virtual void RegisterModelEventHanler(OnModelEventHandler<T, E> handler)
        {
            EventHandler += handler;
            OnLinkView();
        }

        public virtual void UnregisterModelEventHandler(OnModelEventHandler<T, E> handler)
        {
            EventHandler -= handler;
            OnUnlinkView();
        }

        protected abstract void OnLinkView();
        protected abstract void OnUnlinkView();
    }
}
